﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZK.Common;
using ZK.InterfaceServer;

namespace ZK.YHP_TransManage.UC.HardWare
{
    public partial class Index : UserControl
    {
        private string errorStr = string.Empty;
        /// <summary>
        /// 一页显示多少个数据
        /// </summary>
        int OnePageShowList = 10;
        /// <summary>
        /// 页数
        /// </summary>
        int count = 1;
        public Index()
        {
            InitializeComponent();
            aspnetPager1.OnPageChanged += AspnetPager1_OnPageChanged;
        }
        /// <summary>
        /// 添加委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AspnetPager1_OnPageChanged(object sender, EventArgs e)
        {
            ShowData(out count);
        }

        private void ShowData(out int count)
        {
            Models.GetHardWareSRceiveM getHandWareSRceiveM = new Models.GetHardWareSRceiveM();
            dataGridView1.DataSource = null;
            getHandWareSRceiveM = AutoControlTransportApiServer.GetHardWareS(new Models.GetHardWareSSendM()
            {
                currentPage = this.count,
                pageSize = OnePageShowList
            });
            if (getHandWareSRceiveM != null && getHandWareSRceiveM.data.Count > 0)
            {
                dataGridView1.AutoGenerateColumns = true; int nums;
                string txtCurrentPage = aspnetPager1.Controls["panel1"].Controls["txtCurrentPage"].Text.Trim();
                if (string.IsNullOrWhiteSpace(txtCurrentPage)) txtCurrentPage = "1";
                nums = Convert.ToInt32(txtCurrentPage);
                dataGridView1.DataSource = getHandWareSRceiveM.data
                    .Skip(OnePageShowList * (nums - 1))
                    .Take(OnePageShowList)
                    .OrderBy(p => p.ip)
                    .ToList();
                //
                //dataGridView在使用模型绑定时候只需要设定displayName就好， 会自动将其加到列名中。
                //dataGridView1.Columns[nameof(PortModel.Name)].HeaderText = ZK.Common.Utility.GetDisplayName<PortModel>(nameof(PortModel.Name));
                // dataGridView1.Columns[nameof(PortModel.PortName)].HeaderText = ZK.Common.Utility.GetDisplayName<PortModel>(nameof(PortModel.PortName));
                // dataGridView1.Columns[nameof(PortModel.BaudRate)].HeaderText = ZK.Common.Utility.GetDisplayName<PortModel>(nameof(PortModel.BaudRate));
                // dataGridView1.Columns[nameof(PortModel.DataBits)].HeaderText = ZK.Common.Utility.GetDisplayName<PortModel>(nameof(PortModel.DataBits));
                dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCells;
                dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.TopCenter;
                //dataGridView1.Columns[0].Visible = false;//隐藏某一列
                count = getHandWareSRceiveM.data.Count;
            }
            else
            {
                count = 0;
            }

            ShowPage(count);

        }
        /// <summary>
        /// 分页控件
        /// </summary>
        /// <param name="list"></param>
        private void ShowPage(int num)
        {
            aspnetPager1.PageSize = OnePageShowList;//设置显示记录数
            aspnetPager1.DrawControl(num);
        }
        /// <summary>
        /// 删除按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            var nowRow = dataGridView1.CurrentRow;
            string id = nowRow.Cells[0].Value.ToString();

            //if (a.Count>0)
            //{
            //    int a1 = a[0].Index;
            //}
            //else
            //{
            //    int a1 = 0;
            //}
        }
        /// <summary>
        /// 新增按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            var parentPanel = this.Parent.Parent.Controls;
            parentPanel[1].Controls.Clear();
            AddUpdate hardWare = new AddUpdate();
            hardWare.Parent = parentPanel[1];
            hardWare.Dock = DockStyle.Fill;
            hardWare.Show();
        }
        /// <summary>
        /// 修改按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            var nowRow = dataGridView1.CurrentRow;
            string id = nowRow.Cells[0].Value.ToString();
            var parentPanel = this.Parent.Parent.Controls;
            parentPanel[1].Controls.Clear();
            AddUpdate hardWare = new AddUpdate(id);
            hardWare.Parent = parentPanel[1];
            hardWare.Dock = DockStyle.Fill;
            hardWare.Show();
        }
        /// <summary>
        /// search 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button1_Click(object sender, EventArgs e)
        {
            count = 1;
            ShowData(out count);
        }
    }
}
